﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataModel;
using System.Collections.ObjectModel;
using System.Linq;
using A_ManageUsers.Services.Security;
namespace A_ManageUsers.DataModels
{
    public class User:DataModelBase 
    {
        Services.Security.User U;
        public User(Services.Security.User U)
        {
            this.U = U;
            if (U.Roles == null)
            {
                U.Roles = new int[0];
            }
            UserRoles = UserRole.GetUserRoles(U.UserName, U.Roles);
            if (IsNew)
            {
                foreach (var item in UserRoles)
                {
                    item.NewObject();
                }
            }
            Roles.RolesL.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(RolesL_CollectionChanged);
        }



        #region Basic Properties
        public string UserName
        {
            get { return U.UserName; }
            set
            {
                if (U.UserName != value)
                {
                    U.UserName = value;
                    OnPropertyChanged("UserName");
                    
                    if (!base.IsNew)
                    {
                        AddMessage(new ErrorMessage("UserName", "You can't change the username", null));
                    }
                }
            }
        }

        public string Comment
        {
            get { return U.Comment; }
            set
            {
                if (U.Comment != value)
                {
                    base.SetRevertValue("Comment", U.Comment);
                    U.Comment = value;
                    base.SetComitAction("Comment", value, (DMB, s, v) => { UMC.SetUserCommentAsync(UserName, value); });
                    OnPropertyChanged("Comment");
                }
            }
        }

        public string Password { get { return U.Password; } set { U.Password = value; } }
        #endregion

        public ObservableCollection<UserRole> UserRoles { get; set; }
        void RolesL_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.OldItems != null)
            {
                foreach (Role item in e.OldItems)
                {
                    var Removal = UserRoles.First((UR) =>  UR.RoleID== item.ID);
                    UserRoles.Remove(Removal);
                }
            }
            if (e.NewItems != null)
            {
                foreach (Role item in e.NewItems)
                {
                    UserRoles.Add(new UserRole(UserName, U.Roles.Contains(item.ID), item));
                }
            }
        }


        protected override void  ComitNewObject()
        {
            U.Roles = UserRoles.Where((UR) => UR.Enabled).Select((UR) => UR.RoleID).ToArray();
            UMC.NewUserAsync(U);
            Users.UsersL.Add(this);
        }

        public override string ToString()
        {
            return (Comment != null && Comment != "")?UserName + " - " + Comment:UserName;
        }

        UserManagementClient UMC = ServiceConfig.NewUserManagementClient();
    }
    public static class Users
    {
        static Users()
        {
            UserManagementClient UMC = ServiceConfig.NewUserManagementClient();


            UMC.GetUsersCompleted += (s, ea) =>
            {
                foreach (var item in ea.Result)
                {
                    UsersL.Add(new User(item));
                }
            };
            UMC.GetUsersAsync();
        }
        public static ObservableCollection<User> UsersL = new ObservableCollection<User>();
    }
}
